﻿using System;
using System.Collections.Generic;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DomainDriver.Presentation.Silverlight.Base;
using AutomotivePricingModel.Companies;
using AutomotivePricingSilverlightApp.Web.Services;

namespace AutomotivePricingSilverlightApp.Controls.Companies
{
    public class CompanyListController : DomainControllerBase
    {
        public const string CompanySelectionName = "CompanySelection";
        private LoadOperation<Company> m_LoadOperation = null;

        public void GetCompanyListAsync()
        {
            CompaniesDomainContext companiesContext = new CompaniesDomainContext();
            EntityQuery<Company> companies = companiesContext.GetCompaniesQuery();
            m_LoadOperation = companiesContext.Load<Company>(companies);
            m_LoadOperation.Completed += new EventHandler(m_LoadOperation_Completed);
        }

        private void m_LoadOperation_Completed(object sender, EventArgs e)
        {
            DispatchRefreshRequestedEvent(m_LoadOperation);
        }

        public Company GetSelectedCompany()
        {
            Company storedSelection = null;
            if (UiConfiguration.ApplicationObject.StateContainer.HasState(CompanySelectionName))
            { storedSelection = UiConfiguration.ApplicationObject.StateContainer.GetStateValue<Company>(CompanySelectionName); }

            return storedSelection;
        }

        public void SetSelectedCompany(Company selection)
        {
            if (!UiConfiguration.ApplicationObject.StateContainer.HasState(CompanySelectionName))
            { UiConfiguration.ApplicationObject.StateContainer.AddState(CompanySelectionName); }

            ApplicationObject.StateContainer.SetStateValue<Company>(CompanySelectionName, selection, this);
        }
    }
}